<?php

class ArticuloTable extends Doctrine_Table
{
  
  public function searchByIndex($text)
  {
    $items = Doctrine_Core::getTable('Articulo');
    $searchObj = $items
        ->getTemplate('Doctrine_Template_Searchable')
        ->getPlugin();

    $searchObj->setOption('analyzer', new MySearchAnalyzer());
    
    $results = $searchObj->search($text);

    $ids = array();
    foreach ($results as $result) {
      $ids[] = $result['id'];
    }
    
    return ( count($ids)>0 ? 
              Doctrine_Query::create()
                ->from('Articulo i')
                ->whereIn('i.id', $ids)
                ->execute() :
              array());
  }

  public function getArticulosActivos(Doctrine_Query $q = null)
  {
    return $this->addArticulosActivosQuery($q)->execute();
  }
 
  public function countArticulosActivos(Doctrine_Query $q = null)
  {
    return $this->addArticulosActivosQuery($q)->count();
  }
 
  public function addArticulosActivosQuery(Doctrine_Query $q = null)
  {
    if (is_null($q))
    {
      $q = Doctrine_Query::create()
        ->from('Articulo a');
    }
 
    $alias = $q->getRootAlias();
 
    $q->andWhere($alias . '.publicado = ?', 1)
      ->addOrderBy($alias . '.created_at DESC');
 
    return $q;
  }
	
	
	static public function getLuceneIndex()
	{
	  /*ProjectConfiguration::registerZend();
	 
	  if (file_exists($index = self::getLuceneIndexFile()))
	  {
		  return Zend_Search_Lucene::open($index);
	  }
	 
	  return Zend_Search_Lucene::create($index);*/
	}
	 
	static public function getLuceneIndexFile()
	{
	  //return sfConfig::get('sf_data_dir').'/articulo.'.sfConfig::get('sf_environment').'.index';
	}
	
  public function getForLuceneQuery($query)
  {
    /*$hits = self::getLuceneIndex()->find($query);

    $pks = array();
    foreach ($hits as $hit)
    {
      $pks[] = $hit->pk;
    }
 
	  if (empty($pks))
	  {
	    return array();
	  }
	 
	  $q = $this->createQuery('j')
	    ->whereIn('j.id', $pks)
	    ->limit(20);
	 
	  $q = $this->addArticulosActivosQuery($q);
	 
<<<<<<< .mine
	  return $q->execute();*/
  }

	public function cleanup($days)
	{
	  /*$q = $this->createQuery('a')
	    ->delete()
	    ->andWhere('a.publicado = ?', 0)
	    ->andWhere('a.updated_at < ?', date('Y-m-d', time() - 86400 * $days));
	 
	  return $q->execute();*/
	}
}